package com.jason.frame;

import com.jason.utils.Constants;
import com.jason.utils.WindowUtil;
import sun.awt.image.ToolkitImage;
import sun.awt.image.URLImageSource;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;

/**
 * @Description 四级象限首页
 * @Date 2020/10/12 10:52
 * @Author by 尘心
 */
public class IndexFrame extends JFrame{

    public IndexFrame() throws HeadlessException {

        // 获取当前屏幕尺寸
        Dimension screenSize = WindowUtil.getScreenSize();
        Container contentPane = this.getContentPane();
        contentPane.add(drawPanel());

        URL resource = getClass().getResource("/logo.png");
        // 设置图标
        ToolkitImage toolkitImage = new ToolkitImage(new URLImageSource(resource));
        // 最小化托盘工具
        setMini(this);
        this.setTitle("四级象限时间管理");
        this.setFont(WindowUtil.getFont());
        this.setIconImage(toolkitImage);
        this.setBounds(WindowUtil.generateFrame(screenSize, Constants.FRAME_SIZE_MEDIUM));
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        // 监听关闭时间
        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowIconified(WindowEvent e) {
                dispose();
            }

            @Override
            public void windowClosed(WindowEvent e) {
                // 保存数据
            }
        });
    }

    /**
     * 刻画面板
     */
    private JPanel drawPanel() {
        JPanel panel = new JPanel();
        // 拿到当前面板的尺寸
        GridLayout gridLayout = new GridLayout(2,2,2,2);
        panel.setLayout(gridLayout);

        // 将文本域分成四块
        JTextArea leftUpArea = new JTextArea("重要不紧急\n");
        leftUpArea.setBackground(Color.ORANGE);
        leftUpArea.setFont(WindowUtil.getFont());

        JTextArea leftDownArea = new JTextArea("不紧急不重要\n");
        leftDownArea.setBackground(Color.LIGHT_GRAY);
        leftDownArea.setFont(WindowUtil.getFont());

        JTextArea rightUpArea = new JTextArea("重要紧急\n");
        rightUpArea.setBackground(Color.RED);
        rightUpArea.setFont(WindowUtil.getFont());

        JTextArea rightDownArea = new JTextArea("紧急不重要\n");
        rightDownArea.setBackground(Color.YELLOW);
        rightDownArea.setFont(WindowUtil.getFont());

        // 添加序号
        leftUpArea = appendOrderNum(leftUpArea);
        leftDownArea = appendOrderNum(leftDownArea);
        rightUpArea = appendOrderNum(rightUpArea);
        rightDownArea = appendOrderNum(rightDownArea);

        panel.add(leftUpArea);
        panel.add(rightUpArea);
        panel.add(leftDownArea);
        panel.add(rightDownArea);

        return panel;
    }

    private JTextArea appendOrderNum(JTextArea area) {
        area.append("1. \n");
        area.append("2. \n");
        area.append("3. \n");
        return area;
    }

    private void setMini(JFrame frame) {
        Toolkit tk = Toolkit.getDefaultToolkit();

        URL resource = getClass().getResource("/logo.png");
        Image img = tk.getImage(resource);
        //获得系统托盘的实例
        SystemTray systemTray = SystemTray.getSystemTray();
        TrayIcon trayIcon = null;

        // 创建弹出菜单
        PopupMenu popup = new PopupMenu();

        //退出程序选项
        MenuItem exitItem = new MenuItem("Exit");
        exitItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (JOptionPane.showConfirmDialog(null, "确定退出四级时间规划吗？","退出提示",JOptionPane.YES_NO_OPTION) == 0) {
                    System.exit(0);
                }
            }
        });

        popup.add(exitItem);

        try {
            //设置托盘的图标，*.gif与该类文件同一目录
            trayIcon = new TrayIcon(img, "四级象限时间管理",popup);
            systemTray.add(trayIcon);
            this.setIconImage(img);
            trayIcon.setImageAutoSize(true);
        } catch (AWTException e2) {
            e2.printStackTrace();
        }

        //双击托盘图标，软件正常显示
        trayIcon.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {

                int model = e.getModifiers();
                //双击托盘窗口再现
                if (model != InputEvent.BUTTON3_MASK) {
                    //置此 frame 的状态。该状态表示为逐位掩码。
                    //正常化状态
                    setExtendedState(Frame.NORMAL);
                    setVisible(true);
                }

            }


        });

    }
}
